package com.hq.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.hq.zb.dto.DeptDto;
import com.hq.zb.dto.OrgDto;
import com.hq.zb.exception.BizException;
import com.hq.zb.exception.BizExceptionEnum;
import com.hq.zb.mapper.OrgMapper;
import com.hq.zb.model.Dept;
import com.hq.zb.model.Org;
import com.hq.zb.model.User;
import com.hq.zb.service.ICodeRulesHeaderService;
import com.hq.zb.service.IDeptService;
import com.hq.zb.service.IOrgService;
import com.hq.zb.vo.OrgVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 组织表表 服务实现类
 * </p>
 *
 * @author huanghanrui
 * @since 2022-03-07
 */
@Service
public class OrgServiceImpl extends ServiceImpl<OrgMapper, Org> implements IOrgService {

    @Autowired
    private ICodeRulesHeaderService codeRulesHeaderService;

    @Autowired
    private IDeptService deptService;

    @Autowired
    private OrgMapper orgMapper;

    @Override
    public void saveOrg(Org org) {
        if (StringUtils.isBlank(org.getOrgName())) {
            throw new BizException(BizExceptionEnum.DATA_NOT_NULL, "组织名称不允许为空.");
        }
        org.setEnableFlag(true);
        if (StringUtils.isBlank(org.getOrgCode())) {
            //后台自动生成组织编码
            String code = codeRulesHeaderService.getCode("ORG_CODE", "组织编码规则", "OC", 4);
            org.setOrgCode(code);
        }
        //同时生成一条0级部门信息
        DeptDto dept = new DeptDto();
        if (org.getOrgId() == null) {
            this.insert(org);
        } else {
            this.updateById(org);
            Wrapper<Dept> entityWrapper = new EntityWrapper<>();
            entityWrapper.eq("delete_flag", false);
            entityWrapper.eq("dept_code", org.getOrgCode());
            entityWrapper.eq("org_code", org.getOrgCode());
            List<Dept> depts = deptService.selectList(entityWrapper);
            if (CollectionUtil.isNotEmpty(depts)) {
                dept.setDeptId(depts.get(0).getDeptId());
            }
        }
        dept.setDeptCode(org.getOrgCode());
        dept.setDeptName(org.getOrgName());
        dept.setDeptLevel(0);
        dept.setDeptDirector(org.getOrgDirector());
        dept.setOrgCode(org.getOrgCode());
        dept.setSysSource(org.getSysSource());
        deptService.saveDept(dept);
    }

    @Override
    public Page<OrgVo> listByPage(Page<OrgVo> page, Wrapper<OrgVo> wrapper) {
        page.setRecords(orgMapper.listByPage(page, wrapper));
        return page;
    }

    @Override
    public void deleteOrg(List<Org> orgs) {
        if (CollectionUtil.isNotEmpty(orgs)) {
            for (Org org : orgs) {
                org.setDeleteFlag(true);
                orgMapper.updateById(org);
            }
        }
    }
}
